//
//  MyRefundViewController.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/6.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MyRefundViewController.h"
#import "MyRefundTableViewCell.h"
#import "NSUDManager.h"
#import "RefundListModel.h"
#import "HelpViewController.h"
#import "UITableView+Empty.h"

@interface MyRefundViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;

@end

@implementation MyRefundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"我的退款";
    
    [self rightButton];
    
    [self loadTabbleView];
    
    [self downRefreshRequest];
    [self upRefreshRequest];
}
-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 100, 44);
    [backBtn setImage:IMAGE(@"ic_problem") forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)rightButtonClick{
    HelpViewController *vc = [[HelpViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    [self.tableView registerNib:[UINib nibWithNibName:@"MyRefundTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyRefundTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrData.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *identifier = @"MyRefundTableViewCell";
    MyRefundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    //    MessageModel *model = self.arrData[indexPath.row];
    //    cell.titleName.text = model.title;
    RefundListModel *model = self.arrData[indexPath.row];
    cell.titleLabel.text = model.relatedServiceDetails;
    cell.timeLabel.text = [NSString stringWithFormat:@"发布时间：%@",model.createTime];
    cell.priceLabel.text = [NSString stringWithFormat:@"可退金额：￥%.2f",model.amount];
    if ([model.refundStatus isEqualToString:@"REFUND_STATUS_REFUNDING"]) {
        [cell.typeButton setTitle:@"进行中" forState:UIControlStateNormal];
        [cell.typeButton setTitleColor:UIColorFromRGB(0XFF8804) forState:UIControlStateNormal];
        cell.typeButtonBackView.backgroundColor = UIColorFromRGB(0XFFF3E5);
    }else if ([model.refundStatus isEqualToString:@"REFUND_STATUS_FAILED"]) {
        [cell.typeButton setTitle:@"退款失败" forState:UIControlStateNormal];
        [cell.typeButton setTitleColor:UIColorFromRGB(0XA4A4A4) forState:UIControlStateNormal];
        cell.typeButtonBackView.backgroundColor = UIColorFromRGB(0XF3F3F3);
    }else if ([model.refundStatus isEqualToString:@"REFUND_STATUS_SUCCESS"]) {
        [cell.typeButton setTitle:@"退款成功" forState:UIControlStateNormal];
        [cell.typeButton setTitleColor:UIColorFromRGB(0X60D16B) forState:UIControlStateNormal];
        cell.typeButtonBackView.backgroundColor = UIColorFromRGB(0XEDFFEF);
    }
    if ([model.refundType isEqualToString:@"REFUND_TYPE_WD"]) {
        cell.typeImageView.image = IMAGE(@"ic_问");
        cell.typeLabel.text = @"问答";
        
    }else if ([model.refundType isEqualToString:@"REFUND_TYPE_JM"]) {
        cell.typeImageView.image = IMAGE(@"ic_解");
        cell.typeLabel.text = @"解密私聊";
       
    }else if ([model.refundType isEqualToString:@"REFUND_TYPE_BG"]) {
        cell.typeImageView.image = IMAGE(@"ic_保");
        cell.typeLabel.text = @"入职保过";
        
    }
    return cell;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{

        weakSelf.page = kStartPage;
        [YJMSendRequest postDataWithParameters:@{@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"user/getUserRefundRecord" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                self.arrData = [NSMutableArray arrayWithArray:[RefundListModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]]];
                if (weakSelf.arrData.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];

            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];

                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [YJMSendRequest postDataWithParameters:@{@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"user/getUserRefundRecord" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                NSArray *arrTemp = [RefundListModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"list"]];
                [self.arrData addObjectsFromArray:arrTemp];
                if (arrTemp.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            if (self.arrData.count == 0) {
                [weakSelf.tableView noDataAddEmptyUIWithImageName:@"pic_emptystate_two" andEmptyText:@"暂无相关数据" andTopDistance:130];
                self.tableView.tableFooterView = nil;
            } else {
                [weakSelf.tableView haveDataRemoveEmptyUI];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

